/*
 * @Author: 高瑞寒 15932717091@163.com
 * @Date: 2022-05-17 22:48:58
 * @LastEditTime: 2022-05-20 21:15:21
 * @LastEditors: 高瑞寒 15932717091@163.com
 * @Description:
 */
import router from './router'
import store from '@/store'
// 白名单
const whiteList = ['/login']
router.beforeEach(async(to, from, next) => {
  if (store.getters.token) {
    if (to.path === '/login') {
      next('/')
    } else {
      if (!store.getters.hasUserInfo) {
        const { permission: { menus } } = await store.dispatch('user/getUserInfo')
        const routes = await store.dispatch('permission/filterRouter', menus)
        for (const item of routes) {
          router.addRoute(item)
        }
        return next(to.path)
      }
      next()
    }
  } else {
    if (whiteList.indexOf(to.path) > -1) {
      next()
    } else {
      next('/login')
    }
  }
})
